package com.sh.test;

import com.sh.promise.IPromise;
import com.sh.promise.Promise;

/** 测试 race 第一个改变的promise的状态和终值作为p的状态和终值 ,
 *  并尝试取消其余promise的执行(内部调用future.cancel(true))
 * @Date 16:19 2023/3/10
 **/
public class Demo07 {
    public static void main(String[] args) {
        IPromise p1 = new Promise.Builder().promiseHandler(executor -> {
            Thread.sleep(2000);
            System.out.println("p1执行完成");
            return 1;
        }).build();
        IPromise p2 = new Promise.Builder().promiseHandler(executor -> {
            Thread.sleep(3000);
            int a = 1/0;
            if (!Thread.currentThread().isInterrupted()){
                Thread.sleep(3000);
            }
            System.err.println("p2任务被取消");
            return 2;
        }).build();
        IPromise p3 = new Promise.Builder().promiseHandler(executor -> {
            Thread.sleep(1000);
            System.out.println("p3执行完成");
            return 3;
        }).build();
        long s = System.currentTimeMillis();
        Promise.race(p1,p2,p3).then(resolvedData -> {
            System.out.println("resolvedData:"+resolvedData);
            return null;
        },e->e.printStackTrace());
        System.out.println("耗时："+(System.currentTimeMillis()-s));
    }
}
